//
//  lunBoTableViewCell.m
//  spotrs
//
//  Created by xu on 16/5/11.
//  Copyright © 2016年 TZ. All rights reserved.
//

#import "lunBoTableViewCell.h"

@implementation lunBoTableViewCell
{
    NSTimer *timer;
}
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollerViewWidth = frame.size.width;
        self.scrollerViewHeight = frame.size.height;
        self.dataSource = [NSMutableArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg", nil];
        [self creatView:frame];
        [self configureImageView];
        [self configurePageController];
        timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeBanner) userInfo:nil repeats:YES];
    }
    return self;
}
-(void)changeBanner
{
    self.currentIndex++;
    
    if (self.currentIndex == self.dataSource.count -1) {
        self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex - 1]];
        self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
        self.rightImageView.image = [UIImage imageNamed:self.dataSource.firstObject];
        self.pageControl.currentPage = self.currentIndex;
        self.currentIndex = -1;
    }
    else if (self.currentIndex == self.dataSource.count){
        
        self.leftImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
        self.middleImageView.image = [UIImage imageNamed:self.dataSource.firstObject];
        self.rightImageView.image = [UIImage imageNamed:self.dataSource[1]];
        self.pageControl.currentPage = 0;
        self.currentIndex = 0;
    }
    else if(self.currentIndex == 0){
        self.leftImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
        self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
        self.rightImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex+1]];
        self.pageControl.currentPage = self.currentIndex;
    }else{
        self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex-1]];
        self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
        self.rightImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex+1]];
        self.pageControl.currentPage = self.currentIndex;
    }

}
-(void)creatView:(CGRect)fram
{
    _scrollerView = [[UIScrollView alloc]initWithFrame:(CGRectMake(0, 0, fram.size.width , fram.size.height))];
    _scrollerView.backgroundColor = [UIColor redColor];
    _scrollerView.delegate = self;
    _scrollerView.contentSize = CGSizeMake(self.scrollerViewWidth * 3, self.scrollerViewHeight);
    _scrollerView.contentOffset = CGPointMake(self.scrollerViewWidth, 0);
    _scrollerView.pagingEnabled = YES;
    [self addSubview:_scrollerView];
}
- (void)configureImageView{
    
    self.leftImageView = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, self.scrollerViewWidth, self.scrollerViewHeight))];
    self.middleImageView = [[UIImageView alloc]initWithFrame:(CGRectMake(self.scrollerViewWidth, 0, self.scrollerViewWidth, self.scrollerViewHeight))];
    self.rightImageView = [[UIImageView alloc]initWithFrame:(CGRectMake(2*self.scrollerViewWidth, 0, self.scrollerViewWidth, self.scrollerViewHeight))];
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    if (self.dataSource.count != 0) {
        self.leftImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
        self.middleImageView.image = [UIImage imageNamed:self.dataSource.firstObject];
        self.rightImageView.image = [UIImage imageNamed:self.dataSource[1]];
    }
    [self.scrollerView addSubview:self.leftImageView];
    [self.scrollerView addSubview:self.middleImageView];
    [self.scrollerView addSubview:self.rightImageView];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [timer invalidate];
    timer = nil;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeBanner) userInfo:nil repeats:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offset = scrollView.contentOffset.x;
    if (self.dataSource.count != 0) {
        if (offset >= 2*self.scrollerViewWidth) {
            scrollView.contentOffset = CGPointMake(self.scrollerViewWidth, 0);
            
            self.currentIndex++;
            
            if (self.currentIndex == self.dataSource.count -1) {
                self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex - 1]];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource.firstObject];
                self.pageControl.currentPage = self.currentIndex;
                self.currentIndex = -1;
            }
            else if (self.currentIndex == self.dataSource.count){
                
                self.leftImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource.firstObject];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource[1]];
                self.pageControl.currentPage = 0;
                self.currentIndex = 0;
            }
            else if(self.currentIndex == 0){
                self.leftImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex+1]];
                self.pageControl.currentPage = self.currentIndex;
            }else{
                self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex-1]];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex+1]];
                self.pageControl.currentPage = self.currentIndex;
            }
            
        }
        if (offset <= 0) {
            scrollView.contentOffset = CGPointMake(self.scrollerViewWidth, 0);
            self.currentIndex--;
            if (self.currentIndex == -2) {
                self.currentIndex = self.dataSource.count-2;
                self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.dataSource.count-1]];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
                self.pageControl.currentPage = self.currentIndex;
            }
            else if (self.currentIndex == -1) {
                self.currentIndex = self.dataSource.count-1;
                self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex-1]];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource.firstObject];
                self.pageControl.currentPage = self.currentIndex;
            }else if (self.currentIndex == 0){
                self.leftImageView.image = [UIImage imageNamed:self.dataSource.lastObject];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex+1]];
                self.pageControl.currentPage = self.currentIndex;
            }else{
                self.leftImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex-1]];
                self.middleImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex]];
                self.rightImageView.image = [UIImage imageNamed:self.dataSource[self.currentIndex+1]];
                self.pageControl.currentPage = self.currentIndex;
            }
            
        }
    }
    
}
- (void)configurePageController{
    
    _pageControl = [[UIPageControl alloc]initWithFrame:(CGRectMake(0, _scrollerViewHeight-20, _scrollerViewWidth, 20))];
    _pageControl.numberOfPages = self.dataSource.count;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    _pageControl.userInteractionEnabled = NO;
    [self addSubview:_pageControl];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
